<?php
class Ofx_Plugin_Translate extends Zend_Controller_Plugin_Abstract{
	
	protected $_translate;
	
	public function __construct()
    {    	
    	    			
    }
    //
	public function preDispatch(Zend_Controller_Request_Abstract $request)
    {
    	try {
			$module = $request->getModuleName();
			
			$config = Ofx_Config::getConfig();
			
			//$locale =  $config->resources->translate->locale;
			//echo $locale;
			$session = Zend_Registry::get('session');
			$locale = $session->config["locale"];

			$fileExt = $config->resources->translate->data->fileExt;

	    	$zl = new Zend_Locale();
	    	$zl->setLocale($locale);
			
	    	$lang = $zl->getLanguage();
	    	
	    	//init lang global	
	    	$file = OFX_APP_DIR . '/languages/lang.'.$locale.$fileExt;
	    	//echo $file;
	    	if (file_exists($file) && file_get_contents($file) != '')
				$translate = new Zend_Translate(array(
			        'adapter' => 'array',
			        'content' => $file,
			        'locale'  => $lang
			    ));				
				
	    	//$translate = new Zend_Translate('array', $file , $zl->getLanguage());
	        	        
	        /*
	         * init lang module
	         * Kiem tra $translate cho ton tai khong?  if(!$translate)
	         * - Chua ton tai=> tao moi translate module $translate = new Zend_Translate("Ini", $file, $lang);
	         * - Da ton tai -> addTranslate module	$translate->addTranslation("Ini", $file, $lang); 
	         */	
			           
	        $file = OFX_APP_DIR . '/modules/'.$module.'/languages/lang.'.$locale.$fileExt;
	        
	       
			if (file_exists($file) && file_get_contents($file) != '')
			{
				$translate->addTranslation(array(
			        'adapter' => 'array',
			        'content' => $file,
			        'locale'  => $lang
			    ));
			}
			
			
			/*	
	        $translate->addTranslation(array('adapter' =>'array'
	        									,'content' => OFX_APP_DIR.'/modules/'.$module.'/languages/'.$zl 
						  						,'locale' =>$zl->getLanguage()));
			*/
			Zend_Registry::set('translate', $translate);			
			
			return Zend_Registry::get('translate');	    	
    	}catch (Exception $exp){
			echo "Language erro!";
		}    
    }
}
